package com.yuan.Controller.admin;

import com.yuan.Service.CategoryService;
import com.yuan.dto.CategoryDTO;
import com.yuan.dto.CategoryPageQueryDTO;
import com.yuan.entity.Category;
import com.yuan.result.PageResult;
import com.yuan.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "分类相关接口")

public class CategoryController {

    private final CategoryService categoryService;


    /// 新增分类"
    @PostMapping
    @Operation
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        categoryService.saveSelf(categoryDTO);
        return Result.success();
    }

    //分页查询
    @GetMapping("/page")
    @Operation(description = "分类分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询：{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageSelf(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    //删除分类
    @DeleteMapping
    @Operation(description = "删除分类")
    public Result<String> deleteById(Long id) {
        log.info("删除分类：{}", id);
        categoryService.removeByIdSelf(id);
        return Result.success();

    }

    //修改分类
    @PutMapping
    @Operation(description = "修改分类")
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类:{}", categoryDTO);
        categoryService.updateSelf(categoryDTO);
        return Result.success();
    }


    //启用、禁用分类
    @PostMapping("/status/{status}")
    @Operation(description = "启用、禁用分类")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {

        categoryService.startOrStop(status, id);
        return Result.success();
    }

    //根据类型查询分类
    @GetMapping("/list")
    @Operation(description = "根据类型查询分类")
    public Result<List<Category>> list(Integer type) {

        List<Category> list = categoryService.listSelf(type);

        return Result.success(list);
    }

}




